package com.gopay.gass.service.impl.bank;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.BcdlResult;
import com.gopay.gass.domain.GassMainOrder;
import com.gopay.gass.message.GassPayRespMessage;
import com.gopay.gass.message.ResultCode;
import com.gopay.gass.service.GassOrderService;
import com.gopay.gass.service.impl.GassOrderQueryServiceImpl;
import com.gopay.remote.bank.BcdlInteractRemoteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by HJY on 2015/3/23.
 */
@Service("icbcGassOrderQueryService")
public class ICBCGassOrderQueryServiceImpl extends GassOrderQueryServiceImpl {

    @Resource(name = "bankBcdlClient")
    private BcdlInteractRemoteService bankBcdlClient;

    @Resource(name = "gassOrderService")
    protected GassOrderService gassOrderService;

    @Override
    public GassPayRespMessage query(GassMainOrder main) {
        BcdlResult result = null;
        String resultMsg = "付款失败";
        ResultCode resultCode = ResultCode.get(main.getStlmStatus());
        BcdlMessage message = getMessage(main.getGopayOrderId());
        result = bankBcdlClient.query(message);
        if(result != null){
            if(result.isSuccess()){
                resultCode = ResultCode.SUCCESS;
                resultMsg = "付款成功";
                main.setStlmDes("付款成功");
            }else{
                if(!ProcCodeConstants.PROC_CODE_100E6027.equals(result.getResultCode())){
                    resultCode = ResultCode.UNKNOW;
                    resultMsg = "付款进行中";
                }else{
                    resultCode = ResultCode.FAILED;
                    resultMsg = "付款失败";
                }
                main.setStlmDes(resultMsg);
                main.setRemark1(result.getResultCode()+":"+result.getBankRetMsg());
            }
        }
        if(main.getStlmStatus() != resultCode.getValue()){
            main.setStlmStatus(resultCode.getValue());
            main.setLastUpdTs(new Date());
            gassOrderService.updateMainOrder(main);
        }
        return  createRespMsg(main.getGassId(),resultCode,resultMsg);
    }

    private BcdlMessage getMessage(String gopayOrderId){
        BcdlMessage message = new BcdlMessage();
        message.setBank("ICBC");
        message.setOrderId(gopayOrderId);
        return  message;
    }
}
